#include <QPushButton>
#include <QHBoxLayout>
#include <QCoreApplication>

#include <pluginui/widget/StandardSystemTrayDialog.h>
#include <pluginui/extension/StandardMainDialogExtension.h>
#include <pluginui/widget/StandardAboutDialog.h>
#include <pluginui/widget/StandardPluginListDialog.h>

#include <plugin/util/Framework.h>

namespace antu
{
namespace plugin
{
namespace ui
{
StandardSystemTrayDialog::StandardSystemTrayDialog(PluginContext* pContext, AbstractSecurityContextHandler* pHandler, const QIcon& icon, QWidget* parent/* = nullptr*/)
    : QWidget(parent),
	  m_pContext(pContext)
{
	Q_ASSERT(m_pContext != nullptr);

	QPushButton* pPluginListButton = new QPushButton(tr("Plugins..."), this);
	QPushButton* pAboutButton = new QPushButton(tr("About..."), this);
	QPushButton* pExitButton = new QPushButton(tr("Exit"), this);

	QHBoxLayout* pMainLayout = new QHBoxLayout;
	this->setLayout(pMainLayout);

	QWidget* pContainerWidget = new QWidget;
	pContainerWidget->setContentsMargins(0, 0, 0, 0);
	pMainLayout->addWidget(pContainerWidget, 1);

	QVBoxLayout* pButtonLayout = new QVBoxLayout;
	pMainLayout->addLayout(pButtonLayout);
	pButtonLayout->addWidget(pPluginListButton);
	pButtonLayout->addWidget(pAboutButton);
	pButtonLayout->addWidget(pExitButton);
	pButtonLayout->addStretch(1);

	connect(pPluginListButton, SIGNAL(clicked()), this, SLOT(pluginListClicked()));
	connect(pAboutButton, SIGNAL(clicked()), this, SLOT(aboutClicked()));
	connect(pExitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

	Framework::Instance.setup(this->m_pContext, pHandler);
	Framework::Instance.loadTranslationFile(this->m_oTranslator, "pluginui");

	if (!icon.isNull())
	{
		this->setWindowIcon(icon);
	}
	else
	{
		this->setWindowIcon(QIcon(":/resources/images/heart.png"));
	}

	this->m_pSystemTrayIcon = new QSystemTrayIcon(this);
	this->m_pSystemTrayIcon->setIcon(this->windowIcon());
	this->m_pSystemTrayIcon->setToolTip(this->windowTitle());
	this->setupMenu();

    AbstractExtension* pExtension = new StandardMainDialogExtension(pContainerWidget,this->m_pSystemTrayMenu);
    this->m_pActivator = new GuiPluginActivator(pExtension);

	Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "ctor", "Starting activator");
	this->m_pActivator->init(this);
	this->m_pActivator->start();
	Framework::Instance.logger()->debug(__FILE__, __LINE__, "StandardMainDialog", "ctor", "Started activator");

	this->m_pActivator->finishStartup();

	this->m_sSerial = "0000 0000 0000 0000 0000";
	this->m_oVersion = PluginVersion("1.0.0.0");

    connect(this->m_pSystemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
        this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));

	this->postSetupMenu();
    this->hide();
    this->m_pSystemTrayIcon->show();
}

void StandardSystemTrayDialog::closeEvent( QCloseEvent * pEvent )
{
	if (this->m_pSystemTrayIcon->isVisible())
	{
		this->hide();
		this->m_pSystemTrayIcon->showMessage(this->windowTitle(), tr(""));
		pEvent->ignore();
	}
	else
	{
		CloseSignal signal;
		Framework::Instance.stop(&signal);

		if (signal.canClose())
			QWidget::closeEvent(pEvent);
	}
}

void StandardSystemTrayDialog::pluginListClicked()
{
	StandardPluginListDialog dlg(this);
	dlg.exec();
}

void StandardSystemTrayDialog::aboutClicked()
{
	StandardAboutDialog dlg(this->windowTitle(), this->windowTitle(),
		this->m_oVersion.toString(), this->m_sSerial,
		this->m_oAboutImage, this);
	dlg.exec();
}

void StandardSystemTrayDialog::setupMenu()
{
	this->m_pSystemTrayMenu = new QMenu(this);

	QAction* pShowAction = this->m_pSystemTrayMenu->addAction(tr("Show/Hide"));
	connect(pShowAction, SIGNAL(triggered()), this, SLOT(showClicked()));
	this->m_pSystemTrayMenu->addSeparator();

	this->m_pSystemTrayIcon->setContextMenu(this->m_pSystemTrayMenu);
}

void StandardSystemTrayDialog::postSetupMenu()
{
    if (this->m_pActivator->extension()->menuManager() != nullptr &&
            this->m_pActivator->extension()->menuManager()->hasMenus())
    {
        this->m_pSystemTrayMenu->addSeparator();
    }
	QAction* pPluginListAction = this->m_pSystemTrayMenu->addAction(tr("Plugins..."));
	connect(pPluginListAction, SIGNAL(triggered()), this, SLOT(pluginListClicked()));

	QAction* pAboutAction = this->m_pSystemTrayMenu->addAction(tr("About..."));
	connect(pAboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));

	this->m_pSystemTrayMenu->addSeparator();
	QAction* pExitAction = this->m_pSystemTrayMenu->addAction(tr("Exit"));
	connect(pExitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void StandardSystemTrayDialog::showClicked()
{
	if (!this->isVisible())
	{
		this->showNormal();
	}
	else
	{
		this->hide();
	}
}

void StandardSystemTrayDialog::trayActivated( QSystemTrayIcon::ActivationReason reason )
{
	switch (reason)
	{
	case QSystemTrayIcon::DoubleClick:
		this->showClicked();
		break;
	}
}

}
}
}

